var dataBase_baseTbale;



var dataBase_dataRoot;//当前的年份与月份, 调整后也会跟着调整

var dataBase_current_year;

var dataBase_current_month;

var dataBase_current_dayInMonth;

var dataBase_current_dayInWeek;





//个人的月事(每个月要做的事)

var dataBase_scheduleInMonth;



//月历的纵深

var rowL;



var scheduleOnDayTable;





function initDataBase()

{

	dataBase_getServerTime();

}



/**

 * 本方法获得服务器时间, 并初始化所有的时间参数, 然后初始化剩余的项目

 */

function dataBase_getServerTime()

{

	//一个初始化方法, 这个方法用于读取所有的本月任务

	$.ajax({

		//async:false,

		type: "GET",

		url: "dataBase/getServerTime.php",

		cache: false,

		dataType: "json",

		beforeSend:function(XMLHttpRequest) {},

		success: function(msg){

			dataBase_dataRoot = new Date(msg[0]);

			dataBase_current_year=dataBase_dataRoot.getYear();

			dataBase_current_month=dataBase_dataRoot.getMonth()+1;

			dataBase_current_dayInMonth=dataBase_dataRoot.getDate();

			dataBase_current_dayInWeek=dataBase_dataRoot.getDay();

			

			//创建日历

			dataBase_createDataTable();

			

			dataBase_initDataAndMoveAction();

			//

			//createDayScheduleTable();

			

			//createScheduleOnDayChooseTable();

			

			//createScheduleOnDayTable();

		}

	});

}



/**

 * 本JS的核心方法, 创建日程表

 */

function dataBase_createDataTable()

{

	//获得本月的最大天数

	var maxDayInThisMonth=dataBase_getMaxDate((dataBase_current_year+1900),dataBase_current_month);

	dataBase_baseTbale=$("<div></div>").css({'width':'100%', 'height':'100%','overflow':'hidden'});

	

	

	

	//定义日期行

	var firstLineDiv=$("<div class='dataDivInscheduleWeekMain'></div>");

	

	var monDiv=$("<div class='dataDivInscheduleWeekDay'><div class='dataDivInscheduleWeekDayInside'>星期日</div></div>");

	firstLineDiv.append(monDiv);

	

	var tuDiv=$("<div class='dataDivInscheduleWeekDay'><div class='dataDivInscheduleWeekDayInside'>星期一</div></div>");

	firstLineDiv.append(tuDiv);

	

	var wenDiv=$("<div class='dataDivInscheduleWeekDay'><div class='dataDivInscheduleWeekDayInside'>星期二</div></div>");

	firstLineDiv.append(wenDiv);

	

	var thDiv=$("<div class='dataDivInscheduleWeekDay'><div class='dataDivInscheduleWeekDayInside'>星期三</div></div>");

	firstLineDiv.append(thDiv);

	

	var friDiv=$("<div class='dataDivInscheduleWeekDay'><div class='dataDivInscheduleWeekDayInside'>星期四</div></div>");

	firstLineDiv.append(friDiv);

	

	var satDiv=$("<div class='dataDivInscheduleWeekDay'><div class='dataDivInscheduleWeekDayInside'>星期五</div></div>");

	firstLineDiv.append(satDiv);

	

	var sunDiv=$("<div class='dataDivInscheduleWeekDay'><div class='dataDivInscheduleWeekDayInside'>星期六</div></div>");

	firstLineDiv.append(sunDiv);

	dataBase_baseTbale.append(firstLineDiv);

	

	

	/**

	 * 排列日期

	 */

	var dataNumId=0;//每个格子的编号

	var firstDayInWeek=new Date(dataBase_current_year, dataBase_current_month, 1).getDay();

	var lastDayInPastWeek=new Date(dataBase_current_year, dataBase_current_month-1, 0).getDay();

	var lastDayInCurrentWeek=new Date(dataBase_current_year, dataBase_current_month, 0).getDay();

	if(lastDayInCurrentWeek==0)

	{

		lastDayInCurrentWeek=7;

	}

	var monthMaxLong=new Date(dataBase_current_year, dataBase_current_month, 0).getDate();

	var lastMonthMaxLong=new Date(dataBase_current_year, dataBase_current_month-1, 0).getDate();

	rowL=(lastDayInPastWeek+monthMaxLong+7-lastDayInCurrentWeek)/7;

	var seventhLineDiv=$("<div></div>").css({'width':'100%', 'height':'600px' ,'overflow':'hidden','word-break':'break-all'});

	for(var i=0;i<lastDayInPastWeek;i++)

	{

		var temp=$("<div class='dataDivInscheduleBlank' ref='"+dataNumId+"'><div class='dataDivInscheduleBlankNum'>"+(lastMonthMaxLong-lastDayInPastWeek+i+1)+"<div></div>");

		seventhLineDiv.append(temp);

		dataNumId++;

	}

	for(var i=0;i<monthMaxLong;i++)

	{

		var temp=$("<div class='dataDivInscheduleDay' ref='"+dataNumId+"'><div class='dataDivInscheduleDayNum'>"+(i+1)+"</div></div>");

		//temp.append(createScheduleOnDay);

		temp.append(clock_displayClock);

		//temp.selectable();

		//temp.append(dataBase_addCaseInEachDay);

		seventhLineDiv.append(temp);

		dataNumId++;

	}

	for(var i=0;i<(7-lastDayInCurrentWeek);i++)

	{

		var temp=$("<div class='dataDivInscheduleBlank' ref='"+dataNumId+"'><div class='dataDivInscheduleBlankNum'>"+(i+1)+"</div></div>");

		seventhLineDiv.append(temp);

		dataNumId++;

	}

	dataBase_baseTbale.append(seventhLineDiv);

	

	//最后把所有的数据放入表格中

	dataBase_getPersonalMonthSchedule();

	

	

	$('#scheduleshowputschedule').append(dataBase_baseTbale);

}





//判断月的总日期数

function dataBase_getMaxDate(Y,M)

{

    var d   =   new   Date(   Y   + "/ "+   (M+1)   + "/0 ");

    return d.getDate();

}









/**

 * 核心

 * 把日历事件放入每一个表格里

 * 接获得日历方法

 * 在每日的表格旁边生成一个侧边的表格, 把已有的数据放入, 然后隐藏起来, 当鼠标放上去的时候, 显示.

 */

//获得用的日历, 核心方法

function dataBase_getPersonalMonthSchedule()

{

	//一个初始化方法, 这个方法用于读取所有的本月任务

	$.ajax({

		//async:false,

		type: "GET",

		url: "dataBase/getPersonalMonthSchedule.php",

		data:{'current_year':'20'+dataBase_current_year,'current_month':dataBase_current_month},

		cache: false,

		dataType: "json",

		beforeSend:function(XMLHttpRequest) {},

		success: function(msg){

			dataBase_scheduleInMonth=msg;

			

			dataBase_baseTbale.find('.dataDivInscheduleDay').each(function(){

				

				var time=$(this).find('.dataDivInscheduleDayNum').html();

				

				$(this).find('.tmpMatter').remove();

				

				

				//放入一个隐藏的表单

				var tmptotal=$("<div class='tmpTotal'><div class='tmpTotalTitle'>TA的行程</div></div>");

				//决定这个表单的位置

				

				if(((parseInt($(this).attr("ref"))+1)%7)==0)

				{

					tmptotal.css({'left':($(this).offset().left-$(this).width())+'px'});

				}

				else

				{

					tmptotal.css({'left':($(this).offset().left+$(this).width())+'px'});

				}

				

				if((rowL-Math.floor($(this).attr("ref")/7))==1)

				{

					tmptotal.css({'top':$(this).offset().top-$(this).height()*2+'px'});

				}

				else if((rowL-Math.floor($(this).attr("ref")/7))==2)

				{

					tmptotal.css({'top':$(this).offset().top-$(this).height()*1+'px'});

				}

				else

				{

					tmptotal.css({'top':$(this).offset().top+'px'});

				}

				

				for(var i=0;i<dataBase_scheduleInMonth.length;i++)

				{	

					if(dataBase_scheduleInMonth[i][8]==time)

					{	

						var tmpC=$("<div class='tmpTotalEachBody'></div>");

						var tmpBodyLeft=$("<div class='tmpTotalEachBodyLeft'></div>");

						tmpC.append(tmpBodyLeft);

						var tmpBodyLeftTop=$("<div class='tmpTotalEachBodyLeftTop'>"+dataBase_scheduleInMonth[i][3]+"</div>");

						tmpBodyLeft.append(tmpBodyLeftTop);

						var tmpBodyLeftBot=$("<div class='tmpTotalEachBodyLeftBot'>"+dataBase_scheduleInMonth[i][12]+"</div>");

						tmpBodyLeft.append(tmpBodyLeftBot);

						

						var tmpBodyRight=$("<div class='tmpTotalEachBodyRight'></div>");

						var sys_singleAdd=$("<div class='tmpTotalEachBodyRightImg'><img src='systemImage/sys_singleAdd.jpg'/></div>");

						tmpBodyRight.append(sys_singleAdd);

						tmpC.append(tmpBodyRight);

						

						tmptotal.append(tmpC);

					}



				}

				$(this).parent().append(tmptotal);

				tmptotal.hide();

				$(this).mouseover(function(){

					if(mutex==1)

					{

						return;

					}

					

					$('.tmpTotal').hide();

					tmptotal.show();

				});

				//放入结束

				

				//放入日历的单独表格

				var ic=0;

				for(var i=0;i<dataBase_scheduleInMonth.length;i++)

				{	

					if(dataBase_scheduleInMonth[i][8]==time)

					{	

						var tmpC;

						if(ic>2)

						{

							tmpC=$("<div class='tmpMatter'>......</div>");

							$(this).append(tmpC);

							break;

						}

						

						tmpC=$("<div class='tmpMatter'></div>");

						tmpC.append(dataBase_scheduleInMonth[i][3]);

						tmpC.append(dataBase_scheduleInMonth[i][12]);

						

						$(this).append(tmpC);

						

						ic++;

					}



				}

				//放入结束

				

				

			});

			

		}

	});

}









/**

 * 控制日历左右换月份

 * 然后调用dataBase_getPersonalMonthSchedule()方法更新日历

 * dataBase_initDataAndMoveAction()的辅助方法

 * @returns {Boolean}

 */

function dataBase_moveMonthInSchedule(m)

{

	dataBase_current_month=dataBase_current_month+m;

	if(dataBase_current_month>12)

	{

		dataBase_current_year=dataBase_current_year+1;

		dataBase_current_month=1;

	}

	if(dataBase_current_month<1)

	{

		dataBase_current_year=dataBase_current_year-1;

		dataBase_current_month=12;

	}

	

	return false;

}



/**

 * 初始化日期上的时间年月日

 * 只能执行一次

 * 在初始化的时候执行一次

 */

function dataBase_initDataAndMoveAction()

{

	$('.scheduleshowtimeData').append($("<div class='scheduleshowtimeDataInside'>20"+dataBase_current_year+"年"+dataBase_current_month+"月"+"</div>"));

	

	$('.scheduleshowtimeAdd').click(function(){

		dataBase_moveMonthInSchedule(1);

		dataBase_baseTbale.parent().empty();

		dataBase_createDataTable();

		$('.scheduleshowtimeData').empty();

		$('.scheduleshowtimeData').append($("<div class='scheduleshowtimeDataInside'>20"+dataBase_current_year+"年"+dataBase_current_month+"月"+"</div>"));

	});

	

	$('.scheduleshowtimeSubtract').click(function(){

		dataBase_moveMonthInSchedule(-1);

		dataBase_baseTbale.parent().empty();

		dataBase_createDataTable();

		$('.scheduleshowtimeData').empty();

		$('.scheduleshowtimeData').append($("<div class='scheduleshowtimeDataInside'>20"+dataBase_current_year+"年"+dataBase_current_month+"月"+"</div>"));

	});



	return false;

}











/**

 * 显示互斥事件

 */

var mutex=0;

function dataBase_mutexOn()

{

	mutex=1;

	$('.tmpTotal').hide();

	return;

}



function dataBase_mutexOff()

{

	mutex=0;

	return;

}